#include "AlloyPropertiesDialog.h"

#include <QFormLayout>
#include <QPushButton>
#include <QMessageBox>

#include "Alloy.h"

AlloyPropertiesDialog::AlloyPropertiesDialog(QListWidgetItem *item, QAbstractItemModel *compoundList, QWidget *parent) :
    QDialog(parent)
{
    setModal(true);

    m_list = compoundList;
    m_item = item;

    const Alloy & cm = item->data(Qt::UserRole).value<Alloy>();

    name_LE = new QLineEdit(cm.name);
    Eg_LE = new QLineEdit(QString::number(cm.Eg_bow));
    a_lc_LE = new QLineEdit(QString::number(cm.a_lc_bow));
    delta_so_LE = new QLineEdit(QString::number(cm.delta_so_bow));
    vbo_LE = new QLineEdit(QString::number(cm.vbo_bow));
    mass_ef_LE = new QLineEdit(QString::number(cm.mass_ef_bow));
    ac_LE = new QLineEdit(QString::number(cm.ac_bow));
    av_LE = new QLineEdit(QString::number(cm.av_bow));
    b_LE = new QLineEdit(QString::number(cm.b_bow));
    c11_LE = new QLineEdit(QString::number(cm.c11_bow));
    c12_LE = new QLineEdit(QString::number(cm.c12_bow));

    firstCompoundCB = new QComboBox();
    firstCompoundCB->setModel(m_list);
    firstCompoundCB->setCurrentIndex(cm.firstCompoundIndex.row());

    secondCompoundCB = new QComboBox();
    secondCompoundCB->setModel(m_list);
    secondCompoundCB->setCurrentIndex(cm.secondCompoundIndex.row());

    bowling_label = new QLabel("Parametry wygięcia");

    QFormLayout* form_layout = new QFormLayout();
    form_layout->addRow("Nazwa", name_LE);
    form_layout->addRow(new QLabel("Składniki"));
    form_layout->addRow("Pierwszy", firstCompoundCB);
    form_layout->addRow("Drugi", secondCompoundCB);
    form_layout->addRow(bowling_label);
    form_layout->addRow("Eg", Eg_LE);
    form_layout->addRow("a_lc", a_lc_LE);
    form_layout->addRow("Delta_SO", delta_so_LE);
    form_layout->addRow("VOB", vbo_LE);
    form_layout->addRow("m*", mass_ef_LE);
    form_layout->addRow("a_c", ac_LE);
    form_layout->addRow("a_v" ,av_LE);
    form_layout->addRow("b", b_LE);
    form_layout->addRow("c11", c11_LE);
    form_layout->addRow("c12", c12_LE);



    execute_button = new QPushButton("Ok");
    cancel_button = new QPushButton("Anuluj");

    QVBoxLayout * main_layout = new QVBoxLayout();
    //main_layout->addWidget(bowling_label);
    main_layout->addLayout(form_layout);
    QHBoxLayout * h_layout = new QHBoxLayout();
    h_layout->addWidget(execute_button);
    h_layout->addWidget(cancel_button);
    main_layout->addLayout(h_layout);

    this->setLayout(main_layout);

    connect(execute_button, SIGNAL(clicked()), this, SLOT(populate()));
    connect(cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
}

void AlloyPropertiesDialog::populate()
{
    bool succes_Eg, succes_a_lc, succes_delta_so,
            succes_vbo, succes_mass_ef, succes_ac,
            succes_av, succes_b, succes_c11, succes_c12,
            succes_fCCB, succes_sCCB;

    Alloy temp_alloy;

    temp_alloy.name = name_LE->text();
    temp_alloy.Eg_bow = Eg_LE->text().toDouble(&succes_Eg);
    temp_alloy.a_lc_bow = a_lc_LE->text().toDouble(&succes_a_lc);
    temp_alloy.delta_so_bow = delta_so_LE->text().toDouble(&succes_delta_so);
    temp_alloy.vbo_bow = vbo_LE->text().toDouble(&succes_vbo);
    temp_alloy.mass_ef_bow = mass_ef_LE->text().toDouble(&succes_mass_ef);
    temp_alloy.ac_bow = ac_LE->text().toDouble(&succes_ac);
    temp_alloy.av_bow = av_LE->text().toDouble(&succes_av);
    temp_alloy.b_bow = b_LE->text().toDouble(&succes_b);
    temp_alloy.c11_bow = c11_LE->text().toDouble(&succes_c11);
    temp_alloy.c12_bow = c12_LE->text().toDouble(&succes_c12);

    if ( (firstCompoundCB->currentIndex() != -1)){
        temp_alloy.firstCompoundIndex = m_list->index(firstCompoundCB->currentIndex(), 0);
        succes_fCCB = true;
    }  else{
        succes_fCCB = false;
    }
    if ( secondCompoundCB->currentIndex() != -1){
        temp_alloy.secondCompoundIndex =  m_list->index(secondCompoundCB->currentIndex(), 0);
        succes_sCCB = true;
    }else{
        succes_sCCB = false;
    }
    if (succes_ac && succes_av && succes_Eg && succes_a_lc
            && succes_b && succes_c11 && succes_c12
            && succes_delta_so && succes_mass_ef && succes_vbo
            && succes_fCCB && succes_sCCB) {

        QVariant v;
        v.setValue(temp_alloy);

        m_item->setData(Qt::DisplayRole, name_LE->text());
        m_item->setData(Qt::UserRole, v);

        this->accept();
    } else {
        QString str;
        str = "Niepoprawne dane dla: \n";
        if(!succes_fCCB)
            str += "Pierwszego sładnika \n";
        if(!succes_sCCB)
            str += "Drugiego składnika \n";
        if(!succes_Eg)
            str += "Eg \n";
        if(!succes_a_lc)
            str += "a_lc \n";
        if(!succes_delta_so)
            str += "Delta_SO \n";
        if(!succes_vbo)
            str += "VBO \n";
        if(!succes_mass_ef)
            str += "m* \n";
        if(!succes_ac)
            str += "a_c \n";
        if(!succes_av)
            str += "a_v \n";
        if(!succes_b)
            str += "b \n";
        if(!succes_c11)
            str += "c11 \n";
        if(!succes_c12)
            str += "c12 \n";

        QMessageBox::critical(this,"Blad",str);
    }
}


